package server

import (
	"zk3/common/models"
	"zk3/common/proto/user"
)

func Reg(in *user.RegRequest) (*user.RegResponse, error) {
	u := models.User{
		Mobile: in.Mobile,
		Code:   in.Code,
	}
	err := u.Reg()
	if err != nil {
		return nil, err
	}
	return &user.RegResponse{UserId: int64(u.ID)}, nil
}

func Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	login, err := models.Login(in.Mobile)
	if err != nil {
		return nil, err
	}
	return &user.LoginResponse{UserId: int64(login.ID)}, nil
}

func List(in *user.ListRequest) (*user.ListResponse, error) {
	list, err := models.List(in.Mobile)
	if err != nil {
		return nil, err
	}
	return &user.ListResponse{
		UserId: int64(list.ID),
		Mobile: list.Mobile,
		Code:   list.Code,
	}, nil
}

func UserWorkCreate(in *user.UserWorkCreateRequest) (*user.UserWorkCreateResponse, error) {
	u := models.UserWork{
		Author:      in.Author,
		ReleaseTime: in.ReleaseTime,
	}
	err := u.UserWorkCreate()
	if err != nil {
		return nil, err
	}
	return &user.UserWorkCreateResponse{UserId: int64(u.ID)}, nil
}

func UserGiveCreate(in *user.UserGiveCreateRequest) (*user.UserGiveCreateResponse, error) {
	u := models.UserGive{
		LikeCount: in.LikeCount,
	}
	err := u.UserGiveCreate()
	if err != nil {
		return nil, err
	}
	return &user.UserGiveCreateResponse{UserId: int64(u.ID)}, nil
}

func UserGiveDel(in *user.UserGiveDelRequest) (*user.UserGiveDelResponse, error) {
	del, err := models.UserGiveDel(in.LikeCount)
	if err != nil {
		return nil, err
	}
	return &user.UserGiveDelResponse{UserId: int64(del.ID)}, nil
}

func UserCommentCreate(in *user.UserCommentCreateRequest) (*user.UserCommentCreateResponse, error) {
	u := models.UserComment{
		Author:  in.Author,
		Content: in.Content,
	}
	err := u.UserCommentCreate()
	if err != nil {
		return nil, err
	}
	return &user.UserCommentCreateResponse{UserId: int64(u.ID)}, nil
}
